/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.web;

import com.pokemon.dto.PokemonDTO;
import com.pokemon.dto.TrainerDTO;
import com.pokemon.entities.PokemonType;
import com.pokemon.services.PokemonService;
import com.pokemon.services.TrainerService;
import com.pokemon.web.wrapper.TrainerDTOWrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@UrlBinding("/pokemons/{$event}")
public class PokemonActionBean extends CustomActionBean {
 
    final static Logger log = LoggerFactory.getLogger(PokemonActionBean.class);
 
    @SpringBean
    protected PokemonService pokemonManager;
    @SpringBean
    protected TrainerService trainerManager;
 
    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
        return new ForwardResolution("/pokemon/pokemon.jsp");
    }
 
    public List<PokemonDTO> getPokemons() {
        return pokemonManager.getAllPokemons();
    }
    
    private List<TrainerDTO> trainers = new ArrayList<TrainerDTO>();
    private List<TrainerDTOWrapper> trainers1 = new ArrayList<TrainerDTOWrapper>();
    
    public List<TrainerDTOWrapper> getAllTrainers(){
        trainers = trainerManager.getAllTrainers();
        trainers1.clear();
        for (TrainerDTO t : trainers){
            TrainerDTOWrapper trainer = new TrainerDTOWrapper();
            
            trainer.setId(t.getId());
            trainer.setFullName(t.getFirstName() + " " + t.getLastName());
            System.out.println(trainer.getFullName());
            trainers1.add(trainer);
        }
        
        return trainers1;
    }
    
    public List<PokemonType> getAllTypes() {
        return Arrays.asList(PokemonType.values());
    }
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"add", "save"}, field = "name", required = true),
            @Validate(on = {"add", "save"}, field = "nickName", required = true),
            @Validate(on = {"add", "save"}, field = "reachedLevel", required = true)
    })
    private PokemonDTO pokemon;
 
    public Resolution add() {
        log.debug("add() pokemon={}", pokemon);
        pokemonManager.addPokemon(pokemon);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public PokemonDTO getPokemon() {
        return pokemon;
    }
 
    public void setPokemon(PokemonDTO pokemon) {
        this.pokemon = pokemon;
    }
    
    public Resolution delete() {
        log.debug("delete({})", pokemon.getId());
        pokemonManager.removePokemon(pokemon);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadPokemonFromDatabase() {
        String ids = getContext().getRequest().getParameter("pokemon.id");
        if (ids == null) return;
        pokemon = pokemonManager.getPokemon(Long.parseLong(ids));
    }
 
    public Resolution edit() {
        log.debug("edit() pokemon={}", pokemon);
        return new ForwardResolution("/pokemon/edit.jsp");
    }
 
    public Resolution save() {
        log.debug("save() pokemon={}", pokemon);
        pokemonManager.updatePokemon(pokemon); 
        return new RedirectResolution(this.getClass(), "all");
    }
    
     public Resolution cancel() {
        return new RedirectResolution(this.getClass(), "all");
    }
}